package com.mdd.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IGreetingCardsSetService;
import com.mdd.admin.validate.GreetingCardsSetCreateValidate;
import com.mdd.admin.validate.GreetingCardsSetUpdateValidate;
import com.mdd.admin.validate.GreetingCardsSetSearchValidate;
import com.mdd.admin.vo.GreetingCardsSetListedVo;
import com.mdd.admin.vo.GreetingCardsSetDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.GreetingCardsSet;
import com.mdd.common.mapper.GreetingCardsSetMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 贺卡设置实现类
 * @author DEREK
 */
@Service
public class GreetingCardsSetServiceImpl implements IGreetingCardsSetService {
        
    @Resource
    GreetingCardsSetMapper greetingCardsSetMapper;

    /**
     * 贺卡设置列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<GreetingCardsSetListedVo>
     */
    @Override
    public PageResult<GreetingCardsSetListedVo> list(PageValidate pageValidate, GreetingCardsSetSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<GreetingCardsSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        greetingCardsSetMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:type:str",
            "=:img:str",
            "=:remark:str",
        });

        IPage<GreetingCardsSet> iPage = greetingCardsSetMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<GreetingCardsSetListedVo> list = new LinkedList<>();
        for(GreetingCardsSet item : iPage.getRecords()) {
            GreetingCardsSetListedVo vo = new GreetingCardsSetListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImg(UrlUtils.toAbsoluteUrl(item.getImg()));
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 贺卡设置详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return GreetingCardsSet
     */
    @Override
    public GreetingCardsSetDetailVo detail(Integer id) {
        GreetingCardsSet model = greetingCardsSetMapper.selectOne(
                new QueryWrapper<GreetingCardsSet>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        GreetingCardsSetDetailVo vo = new GreetingCardsSetDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setImg(UrlUtils.toAbsoluteUrl(model.getImg()));
        return vo;
    }

    /**
     * 贺卡设置新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(GreetingCardsSetCreateValidate createValidate) {
        GreetingCardsSet model = new GreetingCardsSet();
        model.setType(createValidate.getType());
        model.setImg(UrlUtils.toRelativeUrl(createValidate.getImg()));
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setRemark(createValidate.getRemark());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        greetingCardsSetMapper.insert(model);
    }

    /**
     * 贺卡设置编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(GreetingCardsSetUpdateValidate updateValidate) {
        GreetingCardsSet model = greetingCardsSetMapper.selectOne(
                new QueryWrapper<GreetingCardsSet>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setType(updateValidate.getType());
        model.setImg(UrlUtils.toRelativeUrl(updateValidate.getImg()));
        model.setRemark(updateValidate.getRemark());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        greetingCardsSetMapper.updateById(model);
    }

    /**
     * 贺卡设置删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        GreetingCardsSet model = greetingCardsSetMapper.selectOne(
                new QueryWrapper<GreetingCardsSet>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        greetingCardsSetMapper.delete(new QueryWrapper<GreetingCardsSet>().eq("id", id));
    }

}
